package additional;

import java.util.Collection;
import java.util.Iterator;
import java.math.BigInteger;
import java.io.PrintWriter;
import edu.ipccenter.toster.*;
import java.util.Arrays;


/**
 *
 * @author BDSHADOW
 */
public class PrintInfofromBean {
    public SuiteUtils su = null;
    public PrintWriter out = null;
    public TestCaseUtils tcu = null;
    
    public PrintInfofromBean(PrintWriter out, SuiteUtils su, TestCaseUtils tcu) {
        this.su = su;
        this.out = out;
        this.tcu = tcu;
    }
    
    public void buildSuiteTree(BigInteger id) {
        //Collection<Suite> children = su.findByID(id).getFirstChildren(id);//это же 
        //Suite, почему нет getFirstChildren (зачем он приватный?)  и что за айди в передаваемых параметрах?
        Collection<Suite> children = su.findByID(id).getFirstChildren(su.findByID(id).getID());
        Iterator<Suite> child = children.iterator();
        
        while (child.hasNext()) {
             Suite s = child.next();
             out.println("<ul type=\"circle\">"); 
             out.println("<li> <a name=\"" + s.getID() + "\" href=\"?suiteToEdit=" + s.getID() + "\" >");
             out.println(s.getName() + "</a>");
             buildSuiteTree(s.getID());
             out.println("</li>");
             out.println("</ul>");
        }
    }
    
    public void getSuiteQuestions(Suite suite) {
        Collection<Name> quests = suite.getTestCases();
        Iterator<Name> quest = quests.iterator();

        while (quest.hasNext()) {
            TestCase tc = tcu.findByID(quest.next().getID());
            out.println("<tr>");
            out.println("<td>");
            out.println(tc.getID());
            out.println("</td>");
            out.println("<td>");
            out.println(tc.getContent());
            out.println("</td>");
            out.println("<td>");
                Collection<Answer> answers = tc.getAnswers();
                Iterator<Answer> answer = answers.iterator();
                out.println("<table>");
                while (answer.hasNext()) {
                    Answer ans = answer.next();
                    out.println("<tr><td>");
                    out.println(ans.getAnsNum() + ")");
                    out.println("</td><td>");
                    if (ans.isCorrect()) {
                        out.print("<b>" + ans.getContent() + "</b>");
                    } else {
                        out.print(ans.getContent());
                    }
                    out.println("</td></tr>");
                }               
                out.println("</table>");  
            out.println("</td>");
            out.println("<td>");
            Collection<Name> themes = tc.getThemes();
            Iterator<Name> theme = themes.iterator();
            while (theme.hasNext()) {
                out.print(theme.next().getName() + ", ");
            }
            out.println("</td>");
            out.println("<td>");
            out.println(tc.getDescription());
            out.println("</td>");
            out.println("</tr>");
        }
    }
    
    public void getAllSuites(BigInteger id, String tab) {
        Collection<Suite> children = su.findByID(id).getFirstChildren(su.findByID(id).getID());
        Iterator<Suite> child = children.iterator();
        
        while (child.hasNext()) {
             Suite s = child.next();
             out.println("<option value=\"" + s.getID() + "\">" + tab + s.getName() + "</option>"); 
             getAllSuites(s.getID(), tab + "&nbsp;&nbsp;");
        }         
    }
}
